//package cn.itxc.demo.zk;
//
//import org.apache.curator.framework.CuratorFramework;
//import org.apache.curator.framework.recipes.cache.ChildData;
//import org.apache.curator.framework.recipes.cache.PathChildrenCache;
//import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//import java.util.HashMap;
//import java.util.Map;
//
///**
// * Created by Gavin on 2016/12/3.
// */
//public class ZookeepConfigCenter {
//
//    private static final String PATH = "/configCenter";
//    private Logger logger = LoggerFactory.getLogger(this.getClass());
//    public void setCuratorFramework(CuratorFramework curatorFramework) {
//        this.curatorFramework = curatorFramework;
//    }
//    private CuratorFramework curatorFramework;
//    private Map<String, String> configMap = new HashMap<>();
//
//
////    public void init() {
////        try {
////            List<String> AllConfigs = curatorFramework.getChildren().forPath(PATH);
////            for ( String config : AllConfigs ) {
////
////                //所有children的key
////                String path = PATH + "/" + config;
////                this.addWatch(curatorFramework,path);
////
////                List<String> configs = curatorFramework.getChildren().forPath(path );
////                for (String childrenConfig : configs) {
////                    byte[] configData = curatorFramework.getData().forPath(path + "/" + childrenConfig);
////                    String configDataValue = new String(configData, Charsets.UTF_8);
////                    logger.debug("Load config key:" + childrenConfig + " value:" + configDataValue);
////                    configMap.put(childrenConfig, configDataValue);
////                }
////            }
////        } catch (Exception e) {
////            logger.error("Load zk config data error", e);
////        }
////    }
//
//
//    /**
//     *
//     * @param client
//     */
//    private void addWatch(CuratorFramework client,String path) {
//        PathChildrenCache childrenCache = new PathChildrenCache(client, path, true);
//        PathChildrenCacheListener childrenCacheListener = (client1, event) -> {
//            ChildData data = event.getData();
//
//            if (data == null){
//                return;
//            }
//
//            String path1 = data.getPath();
//            String node = getNodeName(path1);
//            String evData = new String(data.getData(), "utf-8");
//            switch (event.getType()) {
//                case CHILD_ADDED:
//                    logger.debug("Zk child added:" + path1 +  ",key:" + node  + " data :" + evData);
//                    configMap.put(node, evData);
//                    break;
//                case CHILD_REMOVED:
//                    logger.debug("Zk child removed: " + path1 + ",key:" + node );
//                    configMap.remove(node);
//                    break;
//                case CHILD_UPDATED:
//                    logger.debug("Zk child updated: " + path1 +",key:" + node  +  "  data:" + evData);
//                    configMap.put(node, evData);
//                    break;
//                default:
//                    break;
//            }
//        };
//
//        childrenCache.getListenable().addListener(childrenCacheListener);
//        logger.info("Register zk watcher successfully!");
//        try {
//            childrenCache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT);
//        } catch (Exception e) {
//            logger.error("start children cache error");
//        }
//    }
//
//
//    /**
//     * 从node中取出key
//     * @param path path
//     * @return node的名字
//     */
//    private String getNodeName(String path) {
//        String[] nodes = path.split("/");
//        return nodes[3];
//    }
//
//    public String getConfig(String key) {
//        return this.configMap.get(key);
//    }
//
//
//
//}
